package com.tencreat.mall.controller.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tencreat.common.annotation.Log;
import com.tencreat.common.core.HttpResult;
import com.tencreat.common.core.controller.BaseController;
import com.tencreat.common.core.domain.AjaxResult;
import com.tencreat.common.core.domain.entity.SysUser;
import com.tencreat.common.core.domain.model.ReqStringParam;
import com.tencreat.common.enums.BalanceTypeEnum;
import com.tencreat.common.enums.BusinessType;
import com.tencreat.common.utils.StringUtils;
import com.tencreat.common.utils.poi.ExcelUtil;
import com.tencreat.common.utils.spring.SpringUtils;
import com.tencreat.domain.account.MallUser;
import com.tencreat.domain.mall.GoodsRollOrder;
import com.tencreat.domain.mall.dto.BalanceRecordDTO;
import com.tencreat.domain.mall.dto.RechargeOrderDTO;
import com.tencreat.domain.mall.vo.IntegralStatisticsVO;
import com.tencreat.domain.mall.vo.ResBalanceRecordVO;
import com.tencreat.domain.mall.vo.ResMyPurseInfoVO;
import com.tencreat.service.account.IMallUserService;
import com.tencreat.service.mall.IRechargeOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * r充值(提现)订单Controller
 *
 * @author wangkai
 * @date 2021-11-13
 */
@Api(tags = "充值、余额api")
@RestController
@RequestMapping("/charge/mall")
public class AdminRechargeOrderController extends BaseController {
    @Autowired
    IRechargeOrderService rechargeOrderService;

    @Autowired
    private IMallUserService mallUserService;

    @PostMapping("/pc/recharge")
    @Log(title = "财务管理>账户充值", businessType = BusinessType.INSERT)
    @ApiOperation("pc端充值")
    public HttpResult<?> pcRecharge(@RequestBody Map<String, Object> map) {
        if (StringUtils.isEmpty(String.valueOf(map.get("uuid")))) {
            return HttpResult.error("用户uuid不能为空");
        }

        //1增加2扣除
        if (StringUtils.isEmpty(String.valueOf(map.get("type")))) {
            return HttpResult.error("类型不能为空");
        }

        if (StringUtils.isEmpty(String.valueOf(map.get("value")))) {
            return HttpResult.error("调整金额不能为空");
        }
        return HttpResult.ok(rechargeOrderService.pcRecharge(map));
    }

    @PostMapping("/pc/balanceRecord")
    @ApiOperation("pc端余额记录、货款记录")
    public HttpResult<IPage<ResBalanceRecordVO>> balanceRecord(@RequestBody   @Validated BalanceRecordDTO params) {
        Page<ResBalanceRecordVO> page = new Page<>(params.getPageNum(), params.getPageSize());
        IRechargeOrderService bean = SpringUtils.getBean("rechargeOrderService");
        SysUser user = getLoginCheck().getUser();
        if(user.getUserId() != 1 && user.getUserId() != 2){
            MallUser one = mallUserService.lambdaQuery().eq(MallUser::getMobile, user.getPhonenumber()).one();
            params.setUuid(one.getUuid());
        }
        return HttpResult.ok(bean.balanceRecordPage(page, params));
    }

    @GetMapping("/pc/balanceStatistics")
    @ApiOperation("pc端余额统计")
    public HttpResult<IntegralStatisticsVO> balanceRecord(@RequestParam("balanceType") Integer balanceType) {
        IRechargeOrderService bean = SpringUtils.getBean("rechargeOrderService");
        return HttpResult.ok(bean.balanceStatistics(balanceType));
    }

    /**
     * 导出余额记录列表
     */
    @ApiOperation(value="导出余额记录列表")
    @Log(title = "财务管理>余额记录", businessType = BusinessType.EXPORT)
    @PostMapping("/pc/export")
    public AjaxResult export(@RequestBody BalanceRecordDTO params) {
        params.setPageNum(-1);
        params.setPageSize(-1);
        Page<ResBalanceRecordVO> page = new Page<>(params.getPageNum(), params.getPageSize());
        IRechargeOrderService bean = SpringUtils.getBean("rechargeOrderService");
        Page<ResBalanceRecordVO> resBalanceRecordPage = bean.balanceRecordPage(page, params);
        if (resBalanceRecordPage != null && resBalanceRecordPage.getRecords() != null){
            for (ResBalanceRecordVO r:resBalanceRecordPage.getRecords()) {
                if (r.getType().equals("1")){
                    r.setType("收入");
                }else {
                    r.setType("支出");
                }
                r.setBusinessTypeName(r.getBusinessType().getDesc());
            }
        }
        ExcelUtil<ResBalanceRecordVO> util = new ExcelUtil<ResBalanceRecordVO>(ResBalanceRecordVO.class);
        return util.exportExcel(resBalanceRecordPage.getRecords(), "余额记录");
    }

    @PostMapping("/pc/consumerRollRecord")
    @ApiOperation("pc端消费券记录")
    public HttpResult<IPage<ResBalanceRecordVO>> consumerRollRecord(@RequestBody @Validated BalanceRecordDTO params) {
        Page<ResBalanceRecordVO> page = new Page<>(params.getPageNum(), params.getPageSize());
        IRechargeOrderService bean = SpringUtils.getBean("rechargeOrderService");
        SysUser user = getLoginCheck().getUser();
        if(user.getUserId() != 1 && user.getUserId() != 2){
            params.setUserId(user.getUserId());
        }
        return HttpResult.ok(bean.balanceRecordPage(page, params));
    }

    /**
     * 描述信息  pc端提货单记录
     * @Author yaoruiqi
     * @Date: 2023/10/27 07:05
     */
    @PostMapping("/pc/goodsRollRecord")
    @ApiOperation("pc端提货单记录")
    public HttpResult<IPage<GoodsRollOrder>> goodsRollRecord(@RequestBody @Validated BalanceRecordDTO params) {
        Page<GoodsRollOrder> page = new Page<>(params.getPageNum(), params.getPageSize());
        IRechargeOrderService bean = SpringUtils.getBean("rechargeOrderService");
        SysUser user = getLoginCheck().getUser();
        if(user.getUserId() != 1 && user.getUserId() != 2){
            params.setUserId(user.getUserId());
        }
        return HttpResult.ok(bean.goodsRollRecordPage(page, params));
    }

    /***
     * 描述信息  pc端结算提货单
     * @Author yaoruiqi
     * @Date: 2023/10/27 07:05
     */
    @PostMapping("/pc/updateGoodsRollRecord")
    @ApiOperation("pc端结算提货单")
    public HttpResult<GoodsRollOrder> updateGoodsRollRecord(@RequestBody  GoodsRollOrder params) {
        IRechargeOrderService bean = SpringUtils.getBean("rechargeOrderService");

        return bean.updateGoodsRollRecord(params);
    }


}
